package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.dto.TProjectDto;
import com.ruoyi.system.domain.po.TProjectCategory;
import com.ruoyi.system.domain.po.TProjectCategoryExample;
import com.ruoyi.system.domain.po.TProjectExample;
import com.ruoyi.system.domain.po.TProject;
import com.ruoyi.system.domain.vo.TProjectVo;
import com.ruoyi.system.mapper.TProjectCategoryMapper;
import com.ruoyi.system.mapper.TProjectMapper;
import com.ruoyi.system.service.TProjectService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author nanyang
 */
@Service
public class TProjectServiceImpl implements TProjectService {
    
    @Resource
    private TProjectMapper projectMapper;
    @Resource
    private TProjectCategoryMapper tProjectCategoryMapper;
    
    
    @Override
    public Integer insertProject(TProjectDto.Insert projectInsert) {
        // 新增实体数据
        TProject project = BeanUtil.copyProperties(projectInsert, TProject.class);
        project.setId(IdUtil.getSnowflake().nextId());
        project.setCreateBy(SecurityUtils.getUsername());
        project.setSptStatus(Byte.valueOf("0"));
        project.setNfwStatus(Byte.valueOf("0"));
        project.setCreateTime(new Date());
        return projectMapper.insertSelective(project);
    }

    @Override
    public Integer updateProject(TProjectDto.Update projectUpdate) {
        // 修改实体
        TProject project = BeanUtil.copyProperties(projectUpdate, TProject.class);
        project.setUpdateBy(SecurityUtils.getUsername());
        project.setUpdateTime(new Date());
        return projectMapper.updateByPrimaryKeySelective(project);
    }

    @Override
    public Integer deleteProject(TProjectDto.Delete projectDelete) {
        TProjectExample interfaceExample = new TProjectExample();
        interfaceExample.createCriteria().andIdIn(projectDelete.getIdList());
        TProject project = new TProject();
        project.setDelFlag((byte) 2);
        return projectMapper.updateByExampleSelective(project, interfaceExample);
    }

    @Override
    public List<TProjectVo.Result> listProject(TProjectDto.Query queryProject) {
        TProjectExample projectExample = new TProjectExample();
        TProjectExample.Criteria criteria = projectExample.createCriteria().andDelFlagEqualTo((byte) 0);
        if (StrUtil.isNotBlank(queryProject.getProjectNum())) {
            criteria.andProjectNumLike("%" + queryProject.getProjectNum() + "%");
        }
        if (StrUtil.isNotBlank(queryProject.getTopicName())) {
            criteria.andTopicNameLike("%" + queryProject.getTopicName() + "%");
        }
        if (StrUtil.isNotBlank(queryProject.getTopicShortName())) {
            criteria.andTopicShortNameLike("%" + queryProject.getTopicShortName() + "%");
        }
        if (queryProject.getType() != null) {
            criteria.andTypeEqualTo(queryProject.getType());
        }
        if (StrUtil.isNotBlank(queryProject.getCategory())) {
            criteria.andCategoryLike("%" + queryProject.getCategory() + "%");
        }
        if (StrUtil.isNotBlank(queryProject.getLeadingUnit())) {
            criteria.andLeadingUnitLike("%" + queryProject.getLeadingUnit() + "%");
        }
        if (StrUtil.isNotBlank(queryProject.getPerson())) {
            criteria.andPersonLike("%" + queryProject.getPerson() + "%");
        }
        if (queryProject.getStatus() != null) {
            criteria.andStatusEqualTo(queryProject.getStatus());
        }
        if (queryProject.getSptStatus() != null) {
            criteria.andSptStatusEqualTo(queryProject.getSptStatus());
        }
        if (queryProject.getNfwStatus() != null) {
            criteria.andNfwStatusEqualTo(queryProject.getNfwStatus());
        }
        projectExample.setOrderByClause("create_time desc");
        List<TProject> projectList = projectMapper.selectByExample(projectExample);
        return projectList.stream().map(e -> BeanUtil.copyProperties(e, TProjectVo.Result.class)).collect(Collectors.toList());
    }

    @Override
    public void exportProject(HttpServletResponse response, TProjectDto.Query param) {

    }

    @Override
    public void importProject(List<TProjectDto.Insert> dtoList) {

    }

    @Override
    public TProjectVo.Result queryByNum(String projectNum) {
        TProjectExample projectExample = new TProjectExample();
        projectExample.createCriteria().andDelFlagEqualTo((byte) 0).andProjectNumEqualTo(projectNum);
        List<TProject> tProjects = projectMapper.selectByExample(projectExample);
        if (CollectionUtils.isEmpty(tProjects)) {
            return null;
        }
        return BeanUtil.copyProperties(tProjects.get(0), TProjectVo.Result.class);
    }

    @Override
    public List<TProjectVo.ProjectCategory> queryProjectCategory(String name) {
        TProjectCategoryExample tProjectCategoryExample = new TProjectCategoryExample();
        TProjectCategoryExample.Criteria criteria = tProjectCategoryExample.createCriteria().andDelFlagEqualTo((byte) 0);
        if (StrUtil.isNotEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        List<TProjectCategory> tProjectCategories = tProjectCategoryMapper.selectByExample(tProjectCategoryExample);
        return tProjectCategories.stream().map(e -> BeanUtil.copyProperties(e, TProjectVo.ProjectCategory.class)).collect(Collectors.toList());
    }

    @Override
    public void addCategory(TProjectDto.CategoryAdd dto) {
        TProjectCategory tProjectCategory = BeanUtil.copyProperties(dto, TProjectCategory.class);
        if (tProjectCategory.getId() == null) {
            tProjectCategory.setCreateBy(SecurityUtils.getUsername());
            tProjectCategory.setCreateTime(new Date());
            tProjectCategory.setId(IdUtil.getSnowflake().nextId());
            tProjectCategoryMapper.insertSelective(tProjectCategory);
        }else {
            tProjectCategory.setUpdateBy(SecurityUtils.getUsername());
            tProjectCategory.setUpdateTime(new Date());
            tProjectCategoryMapper.updateByPrimaryKeySelective(tProjectCategory);
        }
    }

    @Override
    public void delCategory(TProjectDto.Delete projectDelete) {
        TProjectCategoryExample interfaceExample = new TProjectCategoryExample();
        interfaceExample.createCriteria().andIdIn(projectDelete.getIdList());
        TProjectCategory project = new TProjectCategory();
        project.setDelFlag((byte) 2);
        tProjectCategoryMapper.updateByExampleSelective(project, interfaceExample);
    }
}
